﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace Swan.SwanWakeUp.Util
{
    [ValueConversion(typeof(DateTime), typeof(int))]
    public class DateTimeConverter : IValueConverter
    {
        DateTime dateTime;

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            dateTime = (DateTime)value;
            int result;
            switch (parameter.ToString().ToLower())
            {
                case "h":
                    result = dateTime.Hour;
                    break;
                case "m":
                    result = dateTime.Minute;
                    break;
                default:
                    result = dateTime.Hour;
                    break;
            }

            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var intValue = (int)value;
            switch (parameter.ToString().ToLower())
            {
                case "h":
                    this.dateTime = new DateTime(this.dateTime.Year, this.dateTime.Month, this.dateTime.Day, intValue, this.dateTime.Minute, this.dateTime.Second);
                    break;
                case "m":
                    this.dateTime = new DateTime(this.dateTime.Year, this.dateTime.Month, this.dateTime.Day, this.dateTime.Hour, intValue, this.dateTime.Second);
                    break;
                default:
                    break;
            }

            return this.dateTime;
        }
    }
}
